//
//  UIBarButtonItem+Extension.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/9/22.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
     convenience init(title : String = "" , imageName : String? = nil , target : Any , action : Selector) {
        
        let btn = UIButton()
        //设置图片
        if imageName != nil {
            
            btn.setImage(UIImage(named : imageName!), for: .normal)
            btn.setImage(UIImage(named : imageName! + "_highlighted"), for: .highlighted)
        }
        
        //设置字体
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(UIColor.gray, for: .normal)
        btn.setTitleColor(UIColor.orange, for: .highlighted)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        //一定要添上自适应,否则不会显示出来
         btn.sizeToFit()
        //添加点击事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        
        self.init()
        //给当前对象设置自定义视图
        self.customView = btn
    }
}
